import java.util.ArrayList;
import java.util.List;

public class JavaCanvas extends JavaClass{
	
	private String className;
	
	private List<BaseDataProperty> baseDataPropertys;
	
	public JavaCanvas(BaseData baseData,String packageName,List<String> imports) {
		super();
		this.className = baseData.getClassName();
		this.packageName = packageName;
		this.imports = imports;
		this.baseDataPropertys = baseData.getBaseDataPropertys();
		init();
	}
	
	public JavaCanvas(String className,List<BaseDataProperty> baseDataPropertys) {
		super();
		this.className = className;
		this.baseDataPropertys = baseDataPropertys;
		init();
	}

	private void init() {
		addBody();
		addConstruct();
		addProperties();
		addToStringFunction();
	}
	
	public static void main(String[] args) {
		BaseDataProperty p1 = new BaseDataProperty("String","qwe","测试1");
		BaseDataProperty p2 = new BaseDataProperty("String","asd","测试2");

		List<BaseDataProperty> list = new ArrayList<BaseDataProperty>();
		list.add(p1);
		list.add(p2);
		JavaCanvas jc = new JavaCanvas("qwe", list);
		jc.readContent();
	}

	private StringBuilder content = new StringBuilder();
	
	/**
	 * 添加属性
	 * @author ye.yangyang
	 * 2018年7月6日
	 * @param baseDataPropertys
	 */
	public void addProperties() {
		StringBuilder sb = new StringBuilder();
		for (BaseDataProperty obj : baseDataPropertys) {
			if(obj.getNotes().isEmpty()) {
				sb.append("\n");
			}else {
				sb.append("\n\t//").append(obj.getNotes().replaceAll("\n|\r", " "));
			}
			sb.append("\n\tprivate ").append(obj.getType()).append(" ").append(obj.getName()).append(";");
		}
		content.insert(getEndInput(), sb);
		sb.setLength(0);//清空
		for (BaseDataProperty obj : baseDataPropertys) {
			sb.append("\n");
			//set
			sb.append("\n\tpublic ").append("void set").append(JavaTool.toUpperCaseFirstOne(obj.getName())).append("(").append(obj.getType()).append(" ").append(obj.getName()).append(") {");
			sb.append("\n\t\tthis.").append(obj.getName()).append(" = ").append(obj.getName()).append(";");
			sb.append("\n\t}");
			//get
			sb.append("\n\n\tpublic ").append(obj.getType()).append(" get").append(JavaTool.toUpperCaseFirstOne(obj.getName())).append("() {");
			sb.append("\n\t\treturn ").append(obj.getName()).append(";");
			sb.append("\n\t}");
		}
		content.insert(getEndInput(), sb);
	}
	
	/**
	 * 添加构造方法
	 * @author ye.yangyang
	 * 2018年7月6日
	 */
	public void addConstruct() {
		StringBuilder sb = new StringBuilder();
		sb.append("\n");
		sb.append("\tpublic ").append(className).append("(){\n\n\t}\n");
		sb.append("\n\tpublic ").append(className).append("(");
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty obj = baseDataPropertys.get(i);
			sb.append(obj.getType());
			sb.append(" ").append(obj.getName());
			if(i!=baseDataPropertys.size()-1) {
				sb.append(",");
			}
		}
		sb.append("){\n");
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty obj = baseDataPropertys.get(i);
			sb.append("\t\tthis.");
			sb.append(obj.getName());
			sb.append(" = ");
			sb.append(obj.getName());
			sb.append(";\n");
		}
		sb.append("\t}");

		content.insert(getEndInput(), sb);
	}
	
	/**
	 * 添加toString 方法
	 * @author ye.yangyang
	 * 2018年7月6日
	 */
	public void addToStringFunction() {
		StringBuilder sb = new StringBuilder();
		sb.append("\n\n\tpublic String toString() {");
		sb.append("\n\t\treturn \"{").append("\\\"").append(className).append("\\\"").append(":{");
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty obj = baseDataPropertys.get(i);
			sb.append("\\\"");
			sb.append(obj.getName());
			sb.append("\\\"");
			sb.append(":\\\"\"+this.");
			sb.append(obj.getName());
			sb.append("+\"\\\"");
			if(i==baseDataPropertys.size()-1) {
			}else {
				sb.append(",");
			}
		}
		sb.append("}}\";\n\t}");

		content.insert(getEndInput(), sb);
	}
	
	private void addBody() {
		content.append("package ").append(packageName).append(";\n\n");
		
		for (String imp : imports) {
			content.append("import ").append(imp).append(";\n");
		}

		content.append("\npublic class ").append(className);
		content.append(" {").append("\n\n}");
	}
	
	private int getEndInput() {
		return content.lastIndexOf("}")-1;
	}
	public void readContent() {
		System.out.println(content);
	}
	

	@Override
	public String toString() {
		return "JavaCanvas [className=" + className + ", baseDataPropertys=" + baseDataPropertys + ", content="
				+ content + "]";
	}

	public StringBuilder getContent() {
		return content;
	}
	
}
